package com.woniuxy.edubureau.controller;

import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.entity.SchoolLeader;
import com.woniuxy.commons.entity.User;
import com.woniuxy.commons.exception.BzeducationException;
import com.woniuxy.edubureau.service.LeaderService;
import com.woniuxy.edubureau.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: luojiwei
 * @date: 2020/7/17 15:14
 * @description: 创建校长的controller
 */
@RestController
@RequestMapping("/leader")
public class LeaderController extends BzeducationException {
    @Autowired
    private LeaderService leaderService;
    @Autowired
    private SchoolService schoolService;

    //创建并指定校长
    @PostMapping("/assign")
    public ResponseEntity<Void> createLeaderInfo(SchoolLeader schoolLeader){
        ResponseEntity<Void> responseEntity=new ResponseEntity<>();
        int res=leaderService.createLeader(schoolLeader);
        if (res>0){
            responseEntity.setMsg("创建校长成功");
            responseEntity.setCode(ResponseEntity.SUCCESS);
            SchoolLeader schoolLeader1=leaderService.getSchoolLeaderById(leaderService.getMaxId());
            User user=new User();
            user.setAccount(schoolLeader1.getAccount());
            user.setPwd(schoolLeader1.getPassword());
            user.setPhone(schoolLeader1.getPhone());
            user.setRole(SchoolLeader.ROLE);
            user.setRid(schoolLeader1.getLid());
            leaderService.add(user);
        }
        return responseEntity;
    }
    //分页查询展示列表
    @GetMapping("/all")
    public ResponseEntity<List<SchoolLeader>> getSchoolLeaders(int page,int limit){
        ResponseEntity<List<SchoolLeader>> responseEntity=new ResponseEntity<>();
        page=(page-1)*limit;
        responseEntity.setCount(leaderService.getSchoolLeaderOfNums());
        List<SchoolLeader> schoolLeaders=leaderService.getSchoolLeaderByPage(page,limit);
        for (SchoolLeader schoolLeader:schoolLeaders) {
            String sname=schoolService.getBySid(schoolLeader.getSid());
            schoolLeader.setSname(sname);
        }
        responseEntity.setData(schoolLeaders);
        return responseEntity;
    }
}
